using Entities;

namespace EntityFramework
{
	public class DatabaseSetup
	{
		public static void Setup()
		{
			using (var db = new EntityFrameworkDemoEntities())
			{
				Clean(db);
				var lynn = GetLynn();
				db.AddToEmployees(lynn);
				db.SaveChanges();
			}
		}	public static void SetupGraduation()
		{
			using (var db = new EntityFrameworkDemoEntities())
			{
				Clean(db);
			    var acme = new Company {Name = "Acme", Website = "www.acme.com"};

			    CreateGraduate(acme, "Sam", "Graduation");
			    CreateGraduate(acme, "Sally", "Graduation");
			    CreateGraduate(acme, "Joe", "Knighthood");
			    CreateGraduate(acme, "Frank", "Knighthood");
			    db.AddToCompanies(acme);
				db.SaveChanges();
			}
		}

	    private static void CreateGraduate(Company acme, string name, string @event)
	    {
	        var sam = new Employee {Name = name, Company1 = acme};
	        sam.Events.Add(new Event() {EventTitle = @event});
	    }

	    private static void Clean(EntityFrameworkDemoEntities db)
		{
			db.ExecuteStoreCommand("Delete From Job");
			db.ExecuteStoreCommand("Delete From Events");
			db.ExecuteStoreCommand("Delete From Employee");
			db.ExecuteStoreCommand("Delete From Company");
			db.SaveChanges();
		}

		private static Employee GetLynn()
		{
			var microsoft = new Company { Name = "Microsoft", Website = "bing.com" };
			var lynn = new Employee { Name = "Lynn", Company1 = microsoft };
			var job = new Job() { Title = "Developer", Status = "Current", Employee1 = lynn };
			var e = new Event() { Employee1 = lynn, EventTitle = "SxSW" };

			return lynn;
		}
	}
}